// pages/homePage/index.js
var app = getApp();
const ip=app.globalData.ip
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopList:[
    ],
    currentOrder:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.StoreList()
    this.getUserCurrentOrder()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.StoreList()
    this.getUserCurrentOrder()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.StoreList()
    this.getUserCurrentOrder()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //加载小店
  StoreList(){
    wx.request({
      url: ip+'/store/StoreList',
      header:{
        token:wx.getStorageSync('token')
      },
      success:res=>{
        console.log(res)
        if(res.data.msg==-1){
                wx.clearStorage({
                  success: (res) => {
                    wx.showToast({
                      title: '请再次授权',
                      icon:'none',
                      duration:30000,
                      success:res=>{
                        wx.switchTab({
                          url: '../userCenter/index',
                          success:res=>{
                            wx.showToast({
                              title: '请再次授权',
                              icon:'none',
                              duration:3000,
                            })
                          }
                        })
                      }
                    })
                  },
                })
        }
        this.setData({
          shopList:res.data.data
        })
      }
    })
  },
  //获取当前订单号
  getUserCurrentOrder(){
    wx.request({
      url: ip+'/store/getUserCurrentOrder',
      data:{
        userId:wx.getStorageSync('userid')
      },
      success:res=>{
          console.log(res.data.data.id)
          this.setData({
            currentOrder:res.data.data.id
          })
      }
    })
  },
  todivid(){
    wx.switchTab({
      url: '../divide/index',
    })
  },
  getAuth(){
    let code=''
    wx.getUserProfile({
      desc: 'desc',
      success:e=>{
        console.log(e)
        this.setData({
          userInfo: e.userInfo,
          buttonHidden:false,
          // hasUserInfo: true,
          code:code,
          iv:e.iv,
          encryptedData:e.encryptedData
        });
        wx.login({
          success(res){
            code=res.code
            wx.request({
              url: ip+"/user/getWxUserOpenid",
              header: {
                "Content-Type": "application/x-www-form-urlencoded"
                },
              data:{
                iv:e.iv,
                encryptedData:e.encryptedData,
                code:code,
              },
              method:'POST',
              success:(res)=>{
                console.log(res)
                  //缓存
                  wx.setStorage({
                    key:"userInfo",
                    data:e.userInfo,
                  });
                  wx.setStorage({
                    key:"openid",
                    data:res.data.data.openid,
                  });
                  wx.setStorage({
                    key:"userid",
                    data:res.data.data.userid,
                  });
                  wx.setStorage({
                    key:"token",
                    data:res.data.data.token,
                  });
                  wx.setStorage({
                    key:"storeid",
                    data:res.data.data.storeid,
                  });
                            //授权后跳转
                  wx.switchTab({
                    url: '../homePage/index',
                  })
                  
              }
            })
          }
          })
      }
    })
    }
})